package org.cloud.admin.auth.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cloud.admin.auth.holder.AppHolder;
import org.cloud.admin.auth.service.SysRoleMenuService;
import org.cloud.admin.auth.service.SysRoleService;
import org.cloud.common.model.R;
import org.cloud.common.util.ValidatorUtils;
import org.cloud.module.common.entity.SysRoleEntity;
import org.cloud.module.common.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色管理
 * @author wangyuan
 * @since 2019年6月5日 上午11:16:31
 *
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysRoleService.queryPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 角色列表
	 */
	@GetMapping("/select")
	public R select(){
		Map<String, Object> map = new HashMap<>();
		
		List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);
		
		return R.ok().put("list", list);
	}
	
	/**
	 * 角色信息
	 */
	@GetMapping("/info/{roleId}")
	public R info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);
		
		role.setStatus(null);
		role.setCreatedUser(null);
		role.setCreatedTime(null);
		role.setUpdatedUser(null);
		role.setUpdatedTime(null);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		
		return R.ok().put("role", role);
	}
	
	/**
	 * 保存角色
	 */
	@PostMapping("/save")
	public R save(@ModelAttribute SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		
		role.setCreatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		role.setUpdatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		sysRoleService.saveRole(role);
		
		return R.ok();
	}
	
	/**
	 * 修改角色
	 */
	@PostMapping("/update")
	public R update(@ModelAttribute SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		
		sysRoleService.update(role);
		
		return R.ok();
	}
	
	/**
	 * 删除角色
	 */
	@PostMapping("/delete")
	public R delete(@RequestParam String roleIds){
	    String[] roleIdArr = roleIds.split(",");
        Long[] roleIdLongArr = new Long[roleIdArr.length];
        for(int i = 0;i < roleIdArr.length;i++) {
            roleIdLongArr[i] = Long.parseLong(roleIdArr[i]);
        }
	    
		sysRoleService.deleteBatch(roleIdLongArr);
		
		return R.ok();
	}
}
